<template>
	<view class="page">
		<u-navbar title="充值记录" border @leftClick="$utils.back()" placeholder />
		<view class="balance">
			<view class="userBox">
				<u-avatar :src='user.avatar' size='100rpx' />
				<view>
					{{user.nick_name}}
				</view>
				<view>
					{{toDay()}}
				</view>
			</view>
			<view class="accountBox">
				<view>
					<view>
						可用余额：<text class="but" @click="jumpRecharge">去充值</text>
					</view>
					<view>
						￥：{{user.account}}
					</view>
				</view>
				<view>
					周{{munberConvert(new Date().getDay())}}
				</view>
			</view>
		</view>
		<view class="body">
			<z-paging ref="paging" style="height: 100%;" :fixed="false" v-model="data" @query="query"
				:default-page-no="1" :default-page-size="10">
				<template v-if="!loading">
					<view style="margin: 10rpx 0;text-align: right;">
						<text style="color: green;margin-right:10rpx ;">收入：+{{recharge}}</text>
						<text style="color: red;">支出：{{expense}}</text>
					</view>
					<view v-for="item in data" :key="item.id" class="userItem">
						<u-icon name='rmb-circle' size='80rpx'  />
						<view class="flex ml20">
							<view style="display: flex;justify-content: space-between;">
								<view style="display: flex;align-items: center;max-width: 60%;">
									<u-text :text='item.title' :lines='1' size='30rpx' />
								</view>
								<view :style="{color:item.balance>0?'green':'red'}">
									￥：<text v-if="item.balance>0">+</text>{{item.balance}}
								</view>
							</view>
							<view style="display: flex;justify-content: space-between;">
								<view style="max-width: 60%;">
									{{item.created_at | changeTime}}
								</view>
								<view>
									余额：￥{{item.remain }}
								</view>
							</view>

						</view>
					</view>
				</template>
				<template v-else>
					<view v-for="item in 10" :key="item" class="userItem">
						<view class="image Skeleton"></view>
						<view class="flex">
							<view style="height: 40rpx;" class="Skeleton"></view>
							<view style="height: 30rpx;" class="Skeleton"></view>
						</view>
					</view>
				</template>
			</z-paging>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				expense: null,
				recharge: null,
				data: [],
				loading: true,
			}
		},
		computed: {
			user() {
				return uni.getStorageSync('user')
			}
		},

		onLoad() {},
		methods: {
			munberConvert(e) {
				switch (e) {
					case 0:
						return '七'
					case 1:
						return '一'
					case 2:
						return '二'
					case 3:
						return '三'
					case 4:
						return '四'
					case 5:
						return '五'
					case 6:
						return '六'
				}
			},
			toDay() {
				const date = new Date();
				const year = date.getFullYear().toString().padStart(4, "0");
				const month = (date.getMonth() + 1).toString().padStart(2, "0");
				const day = date.getDate().toString().padStart(2, "0");
				return `${year}-${month}-${day}`
			},
			query(page, size) {
				let obj = {
					page,
					size
				}
				uni.showLoading({
					title: '加载中...'
				})
				this.$api.My.Balance(obj).then(res => {
					uni.hideLoading()
					console.log(res)
					this.expense = res.data.expense
					this.recharge = res.data.recharge
					this.$refs.paging.complete(res.data.balances.data)
					this.loading = false
				}).catch(err => {
					uni.hideLoading()
					uni.$u.toast(err.data.message)
				})
			},
			jumpRecharge() {
				this.$utils.navigateTo({
					url: '/My/Recharge'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		padding: 0rpx 20rpx;
		height: 100vh;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
	}

	.body {
		flex: 1;
		overflow: hidden;
	}

	.userItem {
		display: flex;
		margin: 20rpx 0 0 0;
		position: relative;


		.flex {
			height: 100rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			padding-bottom: 10rpx;
			box-sizing: border-box;
			border-bottom: 1rpx solid #eee;
			flex: 1;

		}
	}





	.balance {
		background: linear-gradient(to top, #42538E, #4a5df1);
		border-radius: 16rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-around;
		align-items: center;
		margin-top: 20rpx;
		height: 300rpx;
		color: #fff;
		padding: 30rpx 20rpx;
		box-sizing: border-box;

		.userBox {
			display: flex;
			width: 100%;
			align-items: center;

			>view:nth-child(2) {
				flex: 1;
				margin: 0 10rpx;
			}

			view {

				font-size: 40rpx;
			}
		}

		.accountBox {
			display: flex;
			width: 100%;
			align-items: center;
			justify-content: space-between;

			view {
				font-size: 30rpx;
			}

			>view:nth-child(2) {
				margin-right: 100rpx;
			}
		}
	}

	.but {
		border-radius: 100rpx;
		margin-left: 20rpx;
		padding: 5rpx 10rpx;
		background-color: red;
		color: #fff;
	}
</style>